package de.ubschmidt.capi;

import java.nio.ByteBuffer;
import java.util.Arrays;

/**
 *
 */
public class DataB3Ind
        extends CapiPassiveMessage
        implements NcciAccessor {

    private static final int OFFSET_NCCI = OFFSET_MESSAGE;
    private static final int OFFSET_DATA_POINTER = OFFSET_NCCI + SIZE_OF_NCCI;
    private static final int OFFSET_DATA_LENGTH = OFFSET_DATA_POINTER + 4;
    private static final int OFFSET_DATA_HANDLE = OFFSET_DATA_LENGTH + 2;
    private static final int OFFSET_FLAGS = OFFSET_DATA_HANDLE + 2;
    private static final int OFFSET_DATA = OFFSET_FLAGS + 2;

    public DataB3Ind(final ByteBuffer byteBuffer) {
        super(byteBuffer);
    }

    /** {@inheritDoc} */
    public int getNcci() {
        return getByteBuffer().getInt(OFFSET_NCCI);
    }

    // does not make sense in the context of a Java CAPI
    //
    // public int getDataPointer() {
    //     return getByteBuffer().getInt(OFFSET_DATA_POINTER);
    // }

    public short getDataLength() {
        return getByteBuffer().getShort(OFFSET_DATA_LENGTH);
    }

    public short getDataHandle() {
        return getByteBuffer().getShort(OFFSET_DATA_HANDLE);
    }

    public Flags getFlags() {
        return new Flags(getByteBuffer().getShort(OFFSET_FLAGS));
    }

    public byte [] getData() {
        byte [] buf = getByteBuffer().array();

        return Arrays.copyOfRange(buf, OFFSET_DATA, buf.length);
    }

    /** {@inheritDoc} */
    @Override
    public String toString() {
        String s = String.format(
                "%s[%s ncci=0x%08x len=%d handle=0x%04x flags=%s]",
                getClass().getSimpleName(),
                super.toString(),
                getNcci(),
                getDataLength(),
                getDataHandle(),
                getFlags());

        return s;
    }

}
